package com.suzhe.ymall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.product.param.SpuPageParam;
import com.suzhe.ymall.product.param.SpuUpdateParam;
import com.suzhe.ymall.product.pojo.Spu;
import com.suzhe.ymall.product.service.ISpuService;
import com.suzhe.ymall.product.vo.SpuVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * spu表 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-07-04
 */
@RestController
@RequestMapping("/product/spu")
@Slf4j
public class SpuController {

    @Autowired
    ISpuService iSpuService;


    @PostMapping("/save")
    @LoginAuth("productSpuSave")
    @ApiOperation(value="保存商品")
    public ApiResEntity save(@RequestBody Spu spu){
        log.debug("save spu:{}",spu);
        iSpuService.saveSpu(spu);
        return ApiResEntity.ok();
    }


    @PostMapping("/update")
    @LoginAuth("productSpuUpdate")
    @ApiOperation(value="修改商品")
    public ApiResEntity update(@RequestBody SpuUpdateParam updateParam){
        log.debug("update SpuUpdateParam:{}",updateParam);
        iSpuService.updateSpu(updateParam);
        return ApiResEntity.ok();
    }

    @GetMapping("/getSpuVo")
    @ApiOperation(value="获取商品")
    public ApiResEntity get(Integer id){
        SpuVo spuVo = iSpuService.getSpuVo(id);
        return ApiResEntity.ok(spuVo);
    }


    @GetMapping("/page")
    @LoginAuth("productSpuView")
    @ApiOperation(value="分页查询商品")
    public ApiResEntity page(SpuPageParam spuPageParam){
        log.debug("page spuPageParam:{}",spuPageParam);
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        if (null != spuPageParam.getId()){
            queryWrapper.eq("id",spuPageParam.getId());
        }
        if (StringUtils.isNotBlank(spuPageParam.getTitle())){
            queryWrapper.like("title",spuPageParam.getTitle());
        }
        if (null != spuPageParam.getSaleable()){
            queryWrapper.eq("saleable",spuPageParam.getSaleable());
        }

        IPage<Spu> page = iSpuService.page(new Page<>(spuPageParam.getPageNo(),//
                spuPageParam.getPageSize()), queryWrapper);

        return ApiResEntity.ok(page);
    }

    @PostMapping("/updateSaleable")
    @LoginAuth("productSpuUpdate")
    @ApiOperation(value="修改商品状态")
    public ApiResEntity updateSaleable(@RequestBody SpuUpdateParam updateParam){
        log.debug("update SpuUpdateParam:{}",updateParam);
        Spu spu = new Spu();
        spu.setId(updateParam.getId());
        spu.setSaleable(updateParam.getSaleable());
        iSpuService.updateById(spu);

        return ApiResEntity.ok();
    }





}
